<?php
namespace Home\Controller;
use Util\AccessController;


/*
 * 微信登录拦截模块
 * 执行后S保存微信用户信息
 *
 */
class LoginController extends AccessController {

    public function index() {
        $wechat = new \Util\WechatTicket();
        $wechat->set(C('WX_APPID'), C('WX_SECRET'));
        $wechat->getCode(C('URL_ROOT') . "Login/Handle?PrevURL={$_GET['PrevURL']}");
    }

    public function Handle() {
        $PrevURL = $_GET['PrevURL'];
        $wechat = new \Util\WechatTicket();
        $wechat->set(C('WX_APPID'), C('WX_SECRET'));
        $userInfo = $wechat->WechatLogin();

        if ($userInfo['errcode'] == 48001) {
            $wechat->getCode(C('URL_ROOT') . "Login/Handle?PrevURL={$_GET['PrevURL']}");
        }
        if ($userInfo && $userInfo['openid']!='C') {
            $this->S($userInfo);
        }else{
            $this->S(null);
        }
        redirect($PrevURL);

    }
}

?>
